package com.javasm.gg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.commons.enums.E;
import com.javasm.gg.Brand;
import com.javasm.gg.BrandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;



@RestController
@RequestMapping("brand")
public class BrandController extends BaseController {
    @Resource
    private BrandService bs;

    @GetMapping("distinctBrandName")
    public AxiosResult getDistinctBrand(){
        QueryWrapper w = new QueryWrapper();
        w.select("distinct brand_name,id");
        List<Brand> list = bs.list(w);
        return AxiosResult.suc(list);
    }

    //查询所有信息和分页
    @GetMapping("getBrandAll")
    public TableDatas getBrand(Brand b){
        startPage();
        List<Brand> getBrand = bs.list(new QueryWrapper<>(b));
        return new TableDatas(getBrand);
    }

    //查询所有信息,不分页
    @GetMapping("BrandAllfy")
    public AxiosResult getBrandAll(){
        List<Brand> list = bs.list();
        return AxiosResult.suc(list);
    }

    //根据id查找数据
    @GetMapping("getbyid")
    public AxiosResult getBrandById(Integer id){
        Brand byId = bs.getById(id);
        return AxiosResult.suc(byId);
    }

    //新增
    @PostMapping
    public AxiosResult insertBrandData(@RequestBody Brand b){
        String brandName = b.getBrandName();
        Brand isIn = bs.getOne(new QueryWrapper<Brand>().eq("brand_name", brandName));
        if(isIn!=null)return AxiosResult.error(E.EXISTS);

        if (bs.save(b))return AxiosResult.suc();
        return AxiosResult.error();
    }

    // 修改数据
    @PutMapping("edit")
    public AxiosResult editBrandManage(@RequestBody Brand b){
        if (bs.updateById(b))return AxiosResult.suc();
        else return AxiosResult.error();
    }

    //删除
    @DeleteMapping("delete/{id}")
    public AxiosResult deleteBrand(@PathVariable Integer id){
        if (bs.removeById(id))return AxiosResult.suc();
        return AxiosResult.error();
    }

    //根据id 删除单条/多条数据
    @DeleteMapping("delectMore/{ids}")
    public AxiosResult delBrandById(@PathVariable("ids") List<Integer> ids) {
        if (bs.removeByIds(ids))return AxiosResult.suc();
        else return AxiosResult.error();
    }

    //根据name模糊查询
    @GetMapping("BrandByName")
    public AxiosResult getByName(Brand name) {
//        List<Brand> getByName = bs.getListBrandName(name);
//        return AxiosResult.suc(getByName);
        return null;
    }
}
